package com.ys.peony.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.model.YWDJPrepareItemModel;
import com.ys.peony.ui.view.wheel.ArrayWheelAdapter;
import com.ys.peony.ui.view.wheel.OnWheelChangedListener;
import com.ys.peony.ui.view.wheel.OnWheelScrollListener;
import com.ys.peony.ui.view.wheel.WheelView;
import com.ys.peony.utils.CommonUtils;

import java.util.List;

/**
 * 0 客户基本信息-职务
 * 1 客户基本信息-婚姻状况
 * 2 客户基本信息-产权状态
 * 3 客户基本信息-单位性质
 * 4 客户基本信息-性别代码
 * 5 车辆信息-产地
 * 6 客户基本信息-是否有产权 0 否 1 是
 * 7 车辆信息-权属标记
 * 8 客户基本信息-行业
 * 下拉选择框
 */
public class TypeValPicker extends PopupWindow  implements OnWheelChangedListener {
    private Context context;
    private View rootView;
    private TypeValCallBack callBack;

    private TextView confirmBtn, cancelBtn;
    private WheelView typeWV;
    //listview 列表
    private List<YWDJPrepareItemModel> options;
    private List<String> optionTextList;
    private int type = 0;//
    private int selectIndex;

    public TypeValPicker(Context context, TypeValCallBack callBack) {
        super();
        this.context = context;
        this.callBack = callBack;
        initView();
        initRegistClickListener();
    }


    private void initView() {
        rootView = LayoutInflater.from(context).inflate(R.layout.pop_typeval_layout, null);
        setContentView(rootView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x61000000);
        setBackgroundDrawable(dw);
        confirmBtn = (TextView) rootView.findViewById(R.id.confirm_btn);
        cancelBtn = (TextView) rootView.findViewById(R.id.cancel_btn);
        typeWV = (WheelView) rootView.findViewById(R.id.type_wv);
        typeWV.setVisibleItems (5);

        rootView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int topheight = rootView.findViewById(R.id.win_content_rl).getTop();
                int leftwidth = rootView.findViewById(R.id.win_content_rl).getLeft();
                int bottomheight = rootView.findViewById(R.id.win_content_rl).getBottom();
                int rightwidth = rootView.findViewById(R.id.win_content_rl).getRight();
                int y = (int) event.getY();
                int x = (int) event.getX();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < topheight || y > bottomheight) {
                        dismiss();
                    }
                    if (x < leftwidth || x > rightwidth) {
                        dismiss();
                    }
                }

                return true;
            }
        });
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {

    }

    public interface TypeValCallBack {
        void pickerTypeValItemOnclick(String text, String val, int type);
    }

    private void initRegistClickListener() {
        typeWV.addChangingListener(this);

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.pickerTypeValItemOnclick(options.get(selectIndex).getText(),options.get(selectIndex).getVal(),type);
                }
                dismiss();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void show(View mRootView, List<YWDJPrepareItemModel> options, int type) {
        this.type = type;
        this.options = options;
        //TODO 设置数据
        optionTextList= CommonUtils.getOptionTextList(options);
        initWheelViewData();
        //修改界面展示
        showAtLocation(mRootView, Gravity.BOTTOM, 0, 0);
    }

    private void initWheelViewData() {
        if (options != null) {
            typeWV.setAdapter(new ArrayWheelAdapter<String>(optionTextList.toArray(new String[options.size()]), options.size()));
            typeWV.setCyclic(false);
            selectIndex=0;
            typeWV.setCurrentItem(0);
            typeWV.addScrollingListener(new OnWheelScrollListener() {

                @Override
                public void onScrollingStarted(WheelView wheel) {

                }

                @Override
                public void onScrollingFinished(WheelView wheel) {
                    selectIndex= wheel.getCurrentItem();
                }
            });
        }
    }
}
